package cn.matezk.lianlian.framework;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.sdk.service.dyvmsapi20170525.AsyncClient;
import com.aliyun.teaopenapi.models.Config;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
public class AliCloudConfig {
    @Value("${aliyun.access-key-id}")
    private String accessKeyId;
    @Value("${aliyun.access-key-secret}")
    private String accessKeySecret;
    @Value("${aliyun.sms.endpoint}")
    private String smsEndpoint;
    @Value("${aliyun.tts.endpoint}")
    private String ttsEndpoint;
    @Value("${aliyun.region}")
    private String region;

    @Bean
    public Client smsClient() throws Exception {
        Config config = new Config().setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret).setEndpoint(smsEndpoint);
        return new Client(config);
    }

    @Bean
    public AsyncClient ttsClient() {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());
        return AsyncClient.builder()
                .region(region)
                .credentialsProvider(provider)
                .overrideConfiguration(ClientOverrideConfiguration.create().setEndpointOverride(ttsEndpoint).setConnectTimeout(Duration.ofSeconds(5)))
                .build();
    }
}
